package com.moco.launcher.play;

import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class MocoPlayerService extends Service {

	private static final String TAG = "MocoPlayerService";

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		MediaController.getInstance().init(this);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// MediaController.getInstance().getDefaultPlayListAndPlay();
		return super.onStartCommand(intent, flags, startId);
	}

	private final IMocoPlayerService.Stub mBinder = new IMocoPlayerService.Stub() {
		@Override
		public void stop() throws RemoteException {
			MediaController.getInstance().stop();
		}

		@Override
		public void playNext() throws RemoteException {
			MediaController.getInstance().playNext();
		}

		@Override
		public void playPrev() throws RemoteException {
			MediaController.getInstance().playPrev();
		}

		@Override
		public void play(List<MusicItemInfo> list) throws RemoteException {
			/*
			 * Log.d(TAG, "play-------------size=" + list.size());
			 * for(MusicItemInfo info:list){ Log.d(TAG, "URL = " +
			 * info.getUrl()); Log.d(TAG, "id =  " + info.getId()); }
			 */
			// MediaController.getInstance().setPlayList(list);
			// MediaController.getInstance().startPlayList(false);
		}

		@Override
		public long getDuration() throws RemoteException {
			return MediaController.getInstance().getDuration();
		}

		@Override
		public long getCurrentPosition() throws RemoteException {
			return MediaController.getInstance().getCurrentPosition();
		}

		@Override
		public void pause() throws RemoteException {
			MediaController.getInstance().pause();
		}

		@Override
		public void seekTo(long msec) throws RemoteException {
			MediaController.getInstance().seekTo(msec);
		}

		@Override
		public void playItem(MusicItemInfo item) throws RemoteException {
			Log.d(TAG, "playItem-------------url" + item.getUrl());
			Log.d(TAG, "playItem-------------id" + item.getId());
			Log.d(TAG, "playItem-------------title" + item.getTitle());
			MediaController.getInstance().playItem(item);
		}

		@Override
		public void setVolume(float leftVolume, float rightVolume)
				throws RemoteException {
			MediaController.getInstance().setVolume(leftVolume, rightVolume);
		}

		@Override
		public void registerStatusChangeCallback(OnStatusChangeCallback callback)
				throws RemoteException {
			MediaController.getInstance()
					.registerStatusChangeCallback(callback);
		}

		@Override
		public void unregisterStatusChangeCallback(
				OnStatusChangeCallback callback) throws RemoteException {
			MediaController.getInstance().unregisterStatusChangeCallback(
					callback);
		}

		@Override
		public boolean isPlaying() throws RemoteException {
			return MediaController.getInstance().isPlaying();
		}

		@Override
		public void start() throws RemoteException {
			MediaController.getInstance().start();
		}

		@Override
		public void setLoopMode(long mode) throws RemoteException {
			MediaController.getInstance().setPlayMode(mode);
		}

		@Override
		public String getCurrentId() throws RemoteException {
			return MediaController.getInstance().getCurrentId(false);
		}

		@Override
		public long getAudioSessionId() throws RemoteException {
			return MediaController.getInstance().getAudioSessionId();
		}

		@Override
		public void setPlayList(List<MusicItemInfo> list)
				throws RemoteException {
			// MediaController.getInstance().setPlayList(list);
		}

		@Override
		public void playCurrent() throws RemoteException {
			MediaController.getInstance().playCurrent();
		}
	};

}